require("zq_screen")
require("TSLib")

init(0)

--判断设备什么分辨率
local width, height = getScreenSize();
local wh_screen;
if width == 1081 and height == 1920 then
	wh_screen = w1080_h1920
elseif width == 720 and height == 1280 then
	wh_screen = w720_h1280
else
	dialog("你的设备暂时还不支持，请联系作者:wolfdong7@qq.com,请选择宽高720*1280,dpi:320的设备",10)
	lua_exit()
end

--打开地图选择
function map_open()
	tap(wh_screen["map_open"][1],wh_screen["map_open"][2])
	mSleep(200)
end

--前往选择的地图
function map_goto()
	tap(wh_screen["map_goto"][1],wh_screen["map_goto"][2])
	mSleep(200)
end

--关闭人物选择，进入地图
function close_goto_map()
	tap(wh_screen["close_goto_map"][1],wh_screen["close_goto_map"][2])
	mSleep(2000)	
end

--进入关卡
function map_guanka()
	tap(wh_screen["map_guanka"][1],wh_screen["map_guanka"][2])
	mSleep(200)
end

--选择关卡人物
function choice_guy(guy)
	for i=0,30,1 do
		x,y = findMultiColorInRegionFuzzyByTable(guy,90,wh_screen["choice_guy_zone"][1],wh_screen["choice_guy_zone"][2],wh_screen["choice_guy_zone"][3],wh_screen["choice_guy_zone"][4])
		if x ~= -1 and y ~= -1 then 
			tap(x,y)
			break
		else		
			mSleep(300)
		end	
	end	
	mSleep(200)
end

--出击
function fight()
	local fight_result = 0;
	for i=0,10,1 do
		--可以出击
		x,y = findMultiColorInRegionFuzzyByTable(wh_screen["fight"],90,0,0,width,height)
		if x ~= -1 and y ~= -1 then 
			tap(x,y)
			fight_result = 1;
			break
		else		
			mSleep(300)
		end	
		--重置出击了
		x,y = findMultiColorInRegionFuzzyByTable(wh_screen["game_over"],90,0,0,width,height)
		if x ~= -1 and y ~= -1 then 			
			fight_result = 2;
			close_goto_map()
			break
		else
			--nLog("出击按键还没找到")
			mSleep(300)
		end	
	end	
	mSleep(200)
	return fight_result;
end

--判断地图
function map_choice(city)
	for i=0,10,1 do	
		moveTo(wh_screen["map_down"][1],wh_screen["map_down"][2],wh_screen["map_down"][3],wh_screen["map_down"][4])
		mSleep(500)			
	end
	
	for i=0,50,1 do
		mSleep(500)
		x,y = findMultiColorInRegionFuzzyByTable(city,90,wh_screen["map_zone"][1],wh_screen["map_zone"][2],wh_screen["map_zone"][3],wh_screen["map_zone"][4])
		if x ~= -1 and y ~= -1 then 
			tap(x,y)
			break
		else
			moveTo(wh_screen["map_move"][1],wh_screen["map_move"][2],wh_screen["map_move"][3],wh_screen["map_move"][4])
			mSleep(1000)
		end		
	end
	
	--没有找到相应的城市，关闭脚本
	if i ==40 then
		lua_exit()
	end
	mSleep(200)
end

--第一次出击，出击多少个英雄
function fight_hero_first(number_temp)		
	local count_hero = 0;
	local hero_temp = 0;
	for k,v in pairs(number_temp) do  
		count_hero = count_hero + 1;
	end 
	nLog("count_hero:"..count_hero)
	
	mSleep(1500);
	if count_hero <=9 and count_hero > 0 then
		--选择出击的英雄
		for i=1,count_hero,1 do
			hero_temp = tonumber(number_temp[i]) + 1;			
			tap(wh_screen["fight_hero"][hero_temp][1],wh_screen["fight_hero"][hero_temp][2]);
			mSleep(500);
		end
		mSleep(1000);
		tap(wh_screen["fight_hero"][10][1],wh_screen["fight_hero"][10][2]);--点击出击，进入游戏
	end
	mSleep(500);
end

--第二次以后出击
function fight_hero_second()	
	mSleep(1500);	
	tap(wh_screen["fight_hero"][10][1],wh_screen["fight_hero"][10][2]);--点击出击，进入游戏
	mSleep(500);
end
--副本

function kaishua(number_temp,city_temp,hero_temp)	
	local fight_flag;
	local fight_times = 0;
	--local fight_number = tonumber(number_temp)
	mSleep(2000);
	mSleep(2000);	
	map_open();
	map_choice(wh_screen[city_temp]);	
	map_goto();
	mSleep(2000);
	map_guanka();
	choice_guy(wh_screen[hero_temp])
	--for local i =0,20,1 do
	--
	while (true) do
		fight_flag = fight();
		if fight_flag == 1 then
			fight_times = fight_times + 1;
			if fight_times == 1 then
				fight_hero_first(number_temp)
			else
				fight_hero_second()
			end
			for i=0,20,1 do
				mSleep(1000);			
				tap(wh_screen["game_touch"][1],wh_screen["game_touch"][2])
				x,y = findMultiColorInRegionFuzzyByTable(wh_screen["game_in"],90,wh_screen["game_in_zone"][1],wh_screen["game_in_zone"][2],wh_screen["game_in_zone"][3],wh_screen["game_in_zone"][4])
				if x ~= -1 and y ~= -1 then 
					mSleep(500)
					tap(wh_screen["menu_open"][1],wh_screen["menu_open"][2])
					mSleep(500)
					tap(wh_screen["menu_finght"][1],wh_screen["menu_finght"][2])
					nLog("自动战斗")
					break;
				else		
					mSleep(300)
				end	
			end
		elseif 	fight_flag == 2 then
			nLog("重置出击,副本刷完")
			mSleep(2000)
			break;
		else
			mSleep(2000)			
			mSleep(1000)
			tap(wh_screen["game_touch"][1],wh_screen["game_touch"][2])
			mSleep(2000)
		end
	end
	--end

end




function test()
	map_open();
end